package com.example.jetour_line.map.mapview;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;

public class LifeListenerManager {
    private  static String TAG = "LifeListenerManager";

    public static void addLifeListener(Context context, LifeListenerFragment.LifeListener mLifeListener ,String tag)  {
        Activity activity = getActivity( context);
        if(activity == null){
            return;
        }
        TAG = TAG +tag;
        LifeListenerFragment fragment = getLifeListenerFragment(activity);
        fragment.addLifeListener(mLifeListener);
    }

    //获取宿主Activity，此处是否有问题？
    public static Activity getActivity(Context context) {
        if (context != null && context instanceof Activity) {
            return (Activity) context;
        }
        return null;
    }
    private static LifeListenerFragment getLifeListenerFragment(Activity activity) {
        FragmentManager manager = activity.getFragmentManager();
        return getLifeListenerFragment(manager);
    }

    //添加空白fragment
    private static LifeListenerFragment getLifeListenerFragment(FragmentManager manager) {

        LifeListenerFragment fragment = (LifeListenerFragment) manager.findFragmentByTag(TAG);;
        if (fragment == null) {
            fragment = new LifeListenerFragment();
            manager.beginTransaction().add(fragment, TAG).commitAllowingStateLoss();
        }

        return fragment;
    }

}
